import 'package:basic_common/common_config.dart';
import 'package:basic_common/common_exports.dart';
import 'package:basic_common/global_info.dart';
import 'package:basic_common/local_storage.dart';
import 'package:basic_common/logger_utils.dart';
// 自定义拦截器
class HttpInterceptor extends Interceptor {
  // 请求拦截
  @override
  void onRequest(
    RequestOptions options,
    RequestInterceptorHandler handler,
  ) {
    options.headers["Project-ID"] = CommonConfig.projectId;
    options.headers["Terminal-ID"] = CommonConfig.terminalId;
    options.headers["Access-Token"] = LocalStorage.get("accessToken") ?? '';
    print("请求的参数==${options.data.toString()}----${options.uri}");
    handler.next(options);
  }

  // 响应拦截
  @override
  Future onResponse(
    Response response,
    ResponseInterceptorHandler handler,
  ) async {
    // do something...
    String uri = response.realUri.path;
    LoggerUtil.v('------------${uri}');
    // 待响应处理
    if (response.data['code'] == -11) {
      ToastUtils.toast(response.data['msg']);
      GlobalInfo.exitApp();
    } else if (response.data['code'] != 1) {
      if (response.data['msg'] != null) {
        ToastUtils.toast(response.data['msg'].toString());
      }
    }
    handler.next(response);
  }

  // 异常拦截
  @override
  Future onError(
    DioException err,
    ErrorInterceptorHandler handler,
  ) async {
    // print("err====${err.response.toString()}");
    // if (err.response != null) {
    // HttpException httpException = HttpException(
    //   code: err.response!.statusCode!,
    //   msg: err.response!.data["message"].toString(),
    // );
    // return httpException;
    // }

    // if (err != null || err.response != null|| ) {
    //   if (err.response!.statusCode == 400) {
    //     EasyLoading.showError(err.response!.data['message'].toString()!);
    //   }
    // }
    // EasyLoading.showError(err.message!);
    handler.next(err);
  }
}
